﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TGB.Core.Infrastructure;
using TGBTech.Core.Models.Services;
using PagedList;

namespace TGBTech.WebAdmin.Controllers
{
    public class SoNhatKyChiThuTienController : Controller
    {
        //session variable
        public static String SESSION_SNKCT_STARTDATE = "session_snkct_startdate";
        public static String SESSION_SNKCT_ENDDATE = "session_snkct_enddate";
        public static String SESSION_SNKCT_LIST = "session_snkct_list";

        //record per page 
        public static int RecordPerNumber = 100;

        private TGBTechDb db = new TGBTechDb();
        
        
        public ActionResult Index()
        {
            return View();
        }
        
        //
        // GET: /SoNhatKyChiThuTien/Create
        public ActionResult Create(int? page)
        {
            //init page number
            int pageNumber = (page ?? 1);
            //get list from session
            var list = Session[SESSION_SNKCT_LIST] as List<TGBTech.Core.Models.Models.BaoCao.SoNhatKyChiThuTienModel.SoNhatKyChiThuTienModel>;
            //check list with null value
            if (list == null)
            {
                list = new List<TGBTech.Core.Models.Models.BaoCao.SoNhatKyChiThuTienModel.SoNhatKyChiThuTienModel>();
            }
            //return to view pagelist 
            return View(list.ToPagedList(pageNumber, RecordPerNumber));
        }

        //
        // POST: /SoNhatKyChiThuTien/Create
        [HttpPost]
        public ActionResult Create(DateTime? StartDate, DateTime? EndDate, int? page, Boolean? isChi)
        {
            // TODO: Add insert logic here
            //Goi services
            int pageNumber = (page ?? 1);
            Boolean IsChi = (isChi ?? true);
            SoNhatKyChiThuTienService service = new SoNhatKyChiThuTienService();
            //init view of taikhoandoiung
            var taikhoandoiung = getInitDicTaiKhoanDoiUng();
            var list = service.createSoNhatKyChiThuTien(StartDate, EndDate, IsChi, taikhoandoiung).ListModel
                .AsQueryable().OrderBy(p => p.NgayThangCT).ToList();
            Session[SESSION_SNKCT_LIST] = list;
            ViewBag.SoChi = IsChi;
            return View("Create", list.ToPagedList(pageNumber, RecordPerNumber));
        }

        //init view of taikhoandoiung
        private Dictionary<String, Decimal> getInitDicTaiKhoanDoiUng()
        {
            var taikhoandoiung = new Dictionary<String, Decimal>();
            return taikhoandoiung;
        }

        [HttpPost]
        public ActionResult Save(DateTime? Date)
        {
            // Save here

            return RedirectToAction("Create");
        }
    }
}
